home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1244 / procapp2.c < prev    next >
C/C++ Source or Header  |  1996-06-17  |  6KB  |  240 lines

  1. #include <windows.h>
  2. #include "procbox.h"
  3. HINSTANCE hinst;
  4.  
  5. int CALLBACK _export PBCallback1(HWND, int , LPARAM);
  6. int CALLBACK _export PBCallback2(HWND, int , LPARAM);
  7.  
  8. ////////////////////////////////////////////////////////////////////////////////
  9. //
  10. //    PROCAPP2.C    - Demonstrates use of ProcessBox Multiple Callback Interface
  11. //                    
  12. //                    - Contains code for 
  13. //                            - WinMain Entry Point
  14. //                            - WndProc
  15.     
  16. LRESULT FAR PASCAL _export WndProc(HWND, UINT, WPARAM, LPARAM);
  17.  
  18.                
  19. char        szAppName[] = "PROCAPP2";
  20. char        szWindowText[] = "ProcessBox - Multiple Callback Modal Interface Demo";
  21.                            
  22.  
  23. /////////////////////////////////////////////////
  24. //
  25. //
  26. //     WndProc     
  27. //
  28.  
  29. LRESULT FAR PASCAL _export WndProc (HWND hwnd, UINT message, 
  30.                                                 WPARAM wParam, LPARAM lParam)
  31. {  
  32.     FARPROC    lpfnPBCallback;
  33.     char        szText[64];     
  34.     HWND        hwndPB;
  35.         
  36.     switch (message)
  37.     {
  38.         case WM_KEYDOWN:                
  39.         case WM_LBUTTONDOWN:                                             
  40.            hwndPB = CreateProcessBox(    hwnd,         // owner
  41.                                                NULL,     // caption
  42.                                                "Callback 1 in process...",         // message
  43.                                                10,50,        // x,y screen coords of box
  44.                                                PB_MESSAGE);
  45.          
  46.            if (hwndPB==NULL)
  47.                return 0;
  48.             ////////////////////////
  49.             //    
  50.             //    FIRST CALLBACK:
  51.             //               
  52.            lpfnPBCallback = MakeProcInstance((FARPROC)PBCallback1, hinst);    
  53.                                                                                
  54.             if (AttachProcess(hwndPB, lpfnPBCallback, NULL)==PBE_OK)
  55.             {            
  56.                 wsprintf(szText, "Returned: %d",
  57.                                 RunProcess(hwndPB));        // returns after process is done
  58.                         
  59.                 MessageBox(hwnd, szText, NULL, NULL);
  60.             }
  61.             else
  62.                 MessageBox(hwnd, "Failed to attach process!", NULL,NULL);
  63.             
  64.             AttachProcess(hwndPB, NULL, NULL);    // detach the process BEFORE freeing it!
  65.             
  66.             FreeProcInstance(lpfnPBCallback);
  67.  
  68.             ////////////////////////
  69.             //    
  70.             //    SECOND CALLBACK:
  71.             //
  72.            lpfnPBCallback = MakeProcInstance((FARPROC)PBCallback2, hinst);    
  73.                                                                                
  74.             if (AttachProcess(hwndPB, lpfnPBCallback, NULL)==PBE_OK)
  75.             {            
  76.                 wsprintf(szText, "Returned: %d",
  77.                                 RunProcess(hwndPB));        // returns after process is done
  78.                         
  79.                 MessageBox(hwnd, szText, NULL, NULL);
  80.             }
  81.             else
  82.                 MessageBox(hwnd, "Failed to attach process!", NULL,NULL);
  83.                         
  84.             DestroyProcessBox(hwndPB);        // DestroyProcessBox BEFORE freeing the proc-instance!!
  85.             
  86.             FreeProcInstance(lpfnPBCallback);    
  87.         return 0;        
  88.  
  89.         case WM_DESTROY:
  90.             PostQuitMessage(0);
  91.         return 0;        
  92.     }
  93.    return DefWindowProc (hwnd, message, wParam, lParam );
  94. }
  95.  
  96. ///////////////////////////////////////////////////////////
  97. //
  98. //    Process Box Callback 1
  99. //
  100. //      
  101.  
  102. int CALLBACK _export PBCallback1(HWND hwndPB, int iCode, LPARAM lParam)
  103. {
  104.     static     i;    
  105.     long        j;       
  106.         
  107.     switch (iCode)
  108.     {
  109.         case PBC_OPEN:
  110.             // allocate memory here
  111.             i=0;
  112.         return TRUE;    
  113.         
  114.         case PBC_CLOSE:
  115.             // free memory here
  116.         return TRUE;
  117.         
  118.         case PBC_CANCEL:
  119.         return (    MessageBox(hwndPB, "Really cancel operation?", "Message Box", 
  120.                         MB_APPLMODAL|MB_YESNO)==IDYES ? TRUE : FALSE );
  121.                             
  122.             
  123.         case PBC_PROCESS:       
  124.             i++;
  125.             if (i==5000) 
  126.             {
  127.                 SendMessage(hwndPB, PM_SETMESSAGE, 0, (LPARAM)(LPSTR)"Callback 1\nReached halfway");
  128.             }
  129.  
  130.             for (j=0; j<0xEFF; j++);    // a delay
  131.  
  132.             if (i>10000)
  133.                 return PBCR_END;                            
  134.             
  135.             SendMessage(hwndPB, PM_SETGAUGE, i/100, 0l);
  136.             return PBCR_CONTINUE;
  137.     }
  138.     return TRUE;
  139. }
  140.  
  141.  
  142. ///////////////////////////////////////////////////////////
  143. //
  144. //    Process Box Callback 2
  145. //
  146. //      
  147.  
  148. int CALLBACK _export PBCallback2(HWND hwndPB, int iCode, LPARAM lParam)
  149. {
  150.     static     i;    
  151.     long        j;       
  152.         
  153.     switch (iCode)
  154.     {
  155.         case PBC_OPEN:
  156.             // allocate memory here
  157.             i=0;
  158.         return TRUE;    
  159.         
  160.         case PBC_CLOSE:
  161.             // free memory here
  162.         return TRUE;
  163.         
  164.         case PBC_CANCEL:
  165.         return (    MessageBox(hwndPB, "Really cancel operation?", "Message Box", 
  166.                         MB_APPLMODAL|MB_YESNO)==IDYES ? TRUE : FALSE );
  167.                             
  168.             
  169.         case PBC_PROCESS:
  170.             i++;
  171.             if (i==1)
  172.             {
  173.                 SendMessage(hwndPB, PM_SETMESSAGE, 0, (LPARAM)(LPSTR)"Second callback\nGoes faster!");
  174.             }
  175.         
  176.             for (j=0; j<0xFF; j++);    // a delay            
  177.             
  178.             if (i>10000)
  179.                 return PBCR_END;
  180.                         
  181.             SendMessage(hwndPB, PM_SETGAUGE, i/100, 0l);
  182.             return PBCR_CONTINUE;        
  183.     }
  184.     return TRUE;
  185. }
  186.  
  187.  
  188. /////////////////////////////////////////////////
  189. //
  190. //
  191. //     WinMain - Entry Point
  192. //
  193. //
  194. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  195.                     LPSTR lpszCmdParam, int nCmdShow)
  196. {     
  197.        
  198.     MSG             msg ;
  199.     WNDCLASS        wndclass ;       
  200.     HWND                hwnd;
  201.     
  202.     hinst = hInstance;        
  203.     if (!hPrevInstance) 
  204.     {
  205.         wndclass.style         = NULL ;
  206.         wndclass.lpfnWndProc   = WndProc ;
  207.         wndclass.cbClsExtra    = 0 ;
  208.         wndclass.cbWndExtra    = 0 ;
  209.         wndclass.hInstance     = hInstance ;
  210.         wndclass.hIcon         = LoadIcon (NULL,IDI_APPLICATION) ;
  211.         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  212.         wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  213.         wndclass.lpszMenuName  = NULL ;
  214.         wndclass.lpszClassName = szAppName ;
  215.                 
  216.         RegisterClass (&wndclass) ;
  217.     }
  218.         
  219.     hwnd = CreateWindow (    szAppName,             // class
  220.                                      szWindowText,            // window text
  221.                                     WS_OVERLAPPEDWINDOW,            // style
  222.                                     CW_USEDEFAULT, CW_USEDEFAULT,        // x, y start
  223.                                     CW_USEDEFAULT, CW_USEDEFAULT,        // width, height
  224.                                     NULL,             // parent window handle
  225.                                     NULL,             // menu handle
  226.                                     hInstance,         // instance handle
  227.                                     NULL) ;            // long pointer to creation data
  228.  
  229.     ShowWindow (hwnd, nCmdShow) ;
  230.     UpdateWindow (hwnd) ;
  231.            
  232.     while (GetMessage (&msg, NULL, 0, 0))
  233.     {
  234.         TranslateMessage (&msg) ;
  235.         DispatchMessage (&msg) ;
  236.     }
  237.     
  238.     return msg.wParam ;   
  239. }  
  240.